<template>
	<view>
		<!-- 绑定微信 -->
		<!-- #ifdef APP-PLUS -->
		<nav-bar title="微信绑定" leftIcon="back" @click-left="back">
			<block slot="right"><button class="cu-btn bg-blue margin-right-sm" @tap="save" :disabled="btndisabled">保存</button></block>
		</nav-bar>
		<!-- #endif -->
		<view class="cu-form-group padding-lr">
			<view class="title" style="width: 150rpx;">微信</view>
			<input type="text" v-model="wechat" placeholder="填入真实微信号，方便您的粉丝和推荐人联系你" placeholder-class="text-gray text-df" />
		</view>
		<!-- #ifdef MP-WEIXIN -->
		<userButton @submitEvent="save" :btnDisabled="btndisabled">保存</userButton>
		<!-- #endif -->
		<loading :loadModal="loadModal"></loading>
	</view>
</template>

<script>
import userButton from '@/components/user-button.vue';
var _self; //this容器
export default {
	components: {
		userButton
	},
	data() {
		return {
			wechat: '',
			oldwechat: '',
			loadModal: false
		};
	},
	computed: {
		btndisabled() {
			if (this.wechat != this.oldwechat && this.wechat.length > 0) {
				return false;
			}
			return true;
		}
	},
	onLoad(options) {
		_self = this;
		const tmpcnt = options.cnt;
		if (tmpcnt) {
			_self.oldwechat = _self.wechat = decodeURIComponent(tmpcnt);
		}
	},
	methods: {
		/* 取消设置 */
		back() {
			uni.navigateBack({ delta: 1 });
		},

		/* 设置用户名 */
		save() {
			const param = {
				wechat: _self.wechat
			};
			_self.loadModal = true;
			_self.$http.httpRequest('user/update', param, true).then(res => {
				_self.loadModal = false;
				if (res._tranStatus) {
					_self.$util.updateUserStorage('wechat', _self.wechat);
					_self.$onFire.fire('wechatListener', encodeURIComponent(_self.wechat));
					_self.back();
				} else {
					uni.showToast({
						icon: 'none',
						title: res._exceptionLocalizedMessage
					});
				}
			});
		}
	}
};
</script>
